Load the data
#getwd() ##Get the present directory, where the data file should reside
#dat_crime <- read_csv(unzip("train.csv.zip"))
##Michael: Since GitHub has an issue storing large files and each time you call 'unzip', it places the large file in the folder. So I loaded the file into a DropBox folder for sharing
dat_crime <- read_csv("https://www.dropbox.com/s/kjkt5ndf3jkibq4/train.csv?raw=1")
#table(dat_crime$Category)
#dat_crime %>% plot(dat_crime, x = dat_crime$X, y = dat_crime$Y, type = "p")
#plot(dat_crime$X, dat_crime$Y, type = "p")
#x <- dat_crime$X ##Latitude
#y <- dat_crime$Y ##Longitude
#plot (x,y)
#dat_crime %>% plot(.)
##Using rworldmap package
#rworld_map <- getMap(resolution = "low")
#plot (rworld_map, xlim = c(-20, 59), ylim = c(35, 71), asp = 1)
##
map_SF <- get_map(location = "San Francisco", zoom = 13)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=San+Francisco&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=San%20Francisco&sensor=false
ggmap(map_SF)

#Different views : http://www.r-bloggers.com/throw-some-throw-some-stats-on-that-mappart-1/
## install.packages("rgbif") ##For insatl
#library("rgbif") #Decided that this is not useful, but leaving here for now
##
map_SF <- get_map(location = "San Francisco", zoom = 13)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=San+Francisco&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=San%20Francisco&sensor=false
##Warning, this one looks like a mess
ggmap(map_SF) +
geom_jitter(data = dat_crime, aes(X, Y), alpha=0.2, size=0.5, color="red")
## Warning: Removed 157431 rows containing missing values (geom_point).

##From: http://www.r-bloggers.com/map-biodiversity-records-with-rgbif-and-ggmap-packages-in-r/
walking_from_1 <- "1 Telegraph Hill Blvd, San Francisco, CA 94133" #Colt Tower
walking_to_1 <- "1201 Mason St, San Francisco, CA 94108" #Cable Car museum
walking_route_1 <- route(walking_from_1, walking_to_1, structure = 'route', mode = 'walking', alternative = FALSE)
## Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=1+Telegraph+Hill+Blvd,+San+Francisco,+CA+94133&destination=1201+Mason+St,+San+Francisco,+CA+94108&mode=walking&units=metric&alternatives=false&sensor=false
map_SF_1 <- get_map(location = "Washington Square Park, San Francisco, CA 94133", zoom = 16)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Washington+Square+Park,+San+Francisco,+CA+94133&zoom=16&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Washington%20Square%20Park,%20San%20Francisco,%20CA%2094133&sensor=false
ggmap(map_SF_1) +
geom_path(data = walking_route_1,
aes(x = lon, y = lat), color="red", size = 1.5, lineend='round'
)
## Warning: Removed 1 rows containing missing values (geom_path).

##Need to figure out how to auto-focus the map
#https://rpubs.com/nickbearman/r-google-map-making
#https://www.google.com/maps/dir/Coit+Tower,+1+Telegraph+Hill+Blvd,+San+Francisco,+CA+94133/Cable+Car+Museum,+Mason+Street,+San+Francisco,+CA/@37.7991066,-122.4139408,16z/data=!3m1!4b1!4m13!4m12!1m5!1m1!1s0x8085808c40000001:0xde85b80121f2dd44!2m2!1d-122.4058222!2d37.8023949!1m5!1m1!1s0x808580f2960c5a5f:0xdfcd6cebc1ae9a35!2m2!1d-122.411488!2d37.7946268
walking_route_1
## m km miles seconds minutes hours leg lon lat
## 1 11 0.011 0.0068354 7 0.1166667 0.001944444 1 -122.4058 37.80274
## 2 204 0.204 0.1267656 205 3.4166667 0.056944444 2 -122.4057 37.80267
## 3 421 0.421 0.2616094 294 4.9000000 0.081666667 3 -122.4080 37.80253
## 4 436 0.436 0.2709304 387 6.4500000 0.107500000 4 -122.4072 37.79879
## 5 379 0.379 0.2355106 300 5.0000000 0.083333333 5 -122.4121 37.79818
## 6 NA NA NA NA NA NA NA -122.4114 37.79481
#Trying to find crime nearby
dat_crime_route_1 <- dat_crime %>% filter(Y <= (walking_route_1$lat[1] + 0.0001),Y >= (walking_route_1$lat[1] - 0.0001),X <= (walking_route_1$lon[1] - 0.001), X >= -122.408)
#dat_crime_route_1 %>% select(X, Y)
##This just renders the crime within the lat/lon constraints of how the map was centered
ggmap(map_SF_1) +
geom_path(data = walking_route_1,
aes(x = lon, y = lat), color="blue", size = 1.5, lineend='round'
) +
geom_jitter(data = dat_crime_route_1, aes(X, Y), alpha=1, size=3, color="red")
## Warning: Removed 1 rows containing missing values (geom_path).
